package com.oauth.twitter.server.utils;

import java.util.ArrayList;
import java.util.List;

import twitter4j.Status;
import twitter4j.User;

import com.rapwaves.feeds.FeedItem;
import com.rapwaves.feeds.FeedOwner;
import com.rapwaves.feeds.Feeds;
import com.rapwaves.feeds.content.FeedContent;
import com.rapwaves.feeds.content.FeedContents;
import com.rapwaves.feeds.content.beans.AbstractContent;
import com.rapwaves.feeds.content.beans.TextContent;

public class FeedConversionUtil {

	public static Feeds getFeeds(List<Status> pUserTimeline) {
		List<FeedItem> feedItems = new ArrayList<FeedItem>();
		if (pUserTimeline != null) {
			for (Status status : pUserTimeline) {
				feedItems.add(getFeedItem(status));
			}
		}
		return new Feeds(feedItems);
	}

	public static FeedItem getFeedItem(Status pStatus) {
		FeedOwner owner = getFeedOwner(pStatus.getUser());
		FeedContents contentList = getFeedContents(pStatus);
		FeedItem feed = new FeedItem(owner, contentList, null);
		return feed;
	}

	private static FeedContents getFeedContents(Status pStatus) {
		List<FeedContent> feedContents = new ArrayList<FeedContent>();
		AbstractContent abstractContent = new TextContent(pStatus.getText());
		FeedContent content = new FeedContent(abstractContent.getContentType(),
				abstractContent);
		feedContents.add(content);
		return new FeedContents(feedContents);

	}

	public static FeedOwner getFeedOwner(User pUser) {
		if (pUser != null) {
			String photoUrl = pUser.getProfileImageURL() != null ? pUser
					.getProfileImageURL().toString() : "";
			String profileUrl = pUser.getURL() != null ? pUser.getURL()
					.toString() : "";
			return new FeedOwner(pUser.getName(), profileUrl, photoUrl);
		}
		return new FeedOwner();
	}
}
